package org.insia.smartposse.displaytag;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import org.displaytag.decorator.TableDecorator;
import org.insia.smartposse.config.DAOFactory;
import org.insia.smartposse.metier.Company;
import org.insia.smartposse.metier.Project;
import org.insia.smartposse.metier.User;
/**
 * Wrapper class for display tag
 * @author Patrick Ferreira
 *
 */
public class Wrapper extends TableDecorator {

    private SimpleDateFormat mDateFormat = null;
    private DecimalFormat mMoneyFormat = null;
    
	/**
	 * Empty constructor
	 *
	 */
    public Wrapper(){
        super();

        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
        if (df instanceof SimpleDateFormat) {
	        SimpleDateFormat sdf = (SimpleDateFormat) df;
	        String pattern = sdf.toLocalizedPattern();
        	if (pattern.indexOf("yyyy") == -1) {
        		df = new SimpleDateFormat(pattern.replaceFirst("yy", "yyyy"));
	        }
        }
        
        this.mDateFormat = (SimpleDateFormat) df;
        this.mMoneyFormat = new DecimalFormat("#,###,###.00 K€");
    }
	
	/***
	 * 
	 * @return une chaine html contenant les action disponible sur le dashboard Project
	 */
    public String getLinkActionDashboardProject()
    {
        Project lObject= (Project)getCurrentRowObject();
        long lId= lObject.getId();

        return "<a href=\"/Smartposse/createProject.do?id=" + lId + "&amp;cmde=edit\"><span class=\"edit\">&nbsp;</span></a>" +
               "<a href=\"/Smartposse/createProject.do?id=" + lId + "&amp;cmde=delete\"><span class=\"delete\">&nbsp;</span></a>";
    }
    
    public String getLinkAddToMe () {
    	String url = "<a href=\"/Smartposse/showProjects.do?id=";
    	Project lObject= (Project)getCurrentRowObject();
    	long lId= lObject.getId();
    	
    	url += lId + "&amp;cmde=addToMe\"><span class=\"add\">&nbsp;</span></a>";
    		
    	return url;
    }
    
	/**
	 * Returns the date as a String in MM/dd/yy format
	 * @return String
	 */
    public String getStart_date() {
    	return this.mDateFormat.format(((Project) this.getCurrentRowObject()).getStart_date());
    }

    /**
     * Returns the money as a String in $ #,###,###.00 format
     * @return String
     */
    public String getBudget()
    {
        return this.mMoneyFormat.format(((Project) this.getCurrentRowObject()).getBudget());
    }
    /**
     * Return the title
     * @return
     */
    public String getTitle()
    {
    	String titre = ((Project)this.getCurrentRowObject()).getTitle();
    	String url = ((Project)this.getCurrentRowObject()).getUrl();
    	
    	if (null != url) 
    	{
    		return "<a href=" + url + " target=\"blank\">" + titre + "</a>&nbsp;<img alt=\"&rarr;\" src=\"../css/images/ico-popup.gif\">";
    	} 
    	return titre;
    }
    /**
     * Return the society or '-' if the current connected user is a freelance 
     * @return
     */
    public String getSociete() {
    	String companyName = "-";
    	
    	try {
    		User creator = ((Project)this.getCurrentRowObject()).getCreator();
    		Company c = DAOFactory.getCompanyDAO().get(creator.getId());
    		companyName = c.getName();
    	} 
    	catch (Exception e) 
    	{
    		// si on a pas réussi à avoir la société c'est qu'on est un freelance et donc y'a rien a faire :p 
    	}
    	
    	return companyName;
    }
    
}